<template>
  <div class="container">
    <el-card shadow="never">
      <div style="float: left">
        <el-form inline>
          <el-form-item label="目录名称">
            <el-input
              style="width: 200px"
              v-model="search"
              size="small"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" label-width="100px">
            <el-select placeholder="请选择" v-model="state" size="small">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="reset" size="small">清除</el-button>
            <el-button type="primary" @click="searchDir" size="small"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div style="float: right">
        <el-button
          type="text"
          v-if="id ? true : false"
          @click="$router.push('./list')"
        >
          <i class="el-icon-back"></i>返回学科</el-button
        >
        <el-button
          type="success"
          icon="el-icon-edit"
          @click="showDialog"
          size="small"
          >新增目录</el-button
        >
      </div>
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin: 0 0 15px"
      >
      </el-alert>
      <!-- 目录列表 -->
      <template>
        <el-table :data="list">
          <el-table-column label="序号" type="index" width="120px">
          </el-table-column>
          <el-table-column label="所属学科" prop="subjectName">
          </el-table-column>
          <el-table-column label="目录名称" prop="directoryName">
          </el-table-column>
          <el-table-column label="创建者" prop="username" width="220px">
          </el-table-column>
          <el-table-column label="创建日期" prop="addDate" width="180px">
            <template slot-scope="{ row }">
              {{ row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column label="面试题数量" prop="totals" width="200px">
          </el-table-column>
          <el-table-column label="状态" prop="state" width="200px">
            <template slot-scope="{ row }">
              <span>{{ row.state === 1 ? "已启用" : "已禁用" }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button type="text" @click="changeState(row.id)">{{
                row.state ? "禁用" : "启用"
              }}</el-button>
              <el-button
                type="text"
                @click="editDirectorys(row)"
                :disabled="row.state ? false : true"
                >修改</el-button
              >
              <el-button
                type="text"
                @click="removeDirectorys(row.id)"
                :disabled="row.state ? false : true"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-row type="flex" justify="center" style="margin: 20px 0 0">
        <el-col :span="6">
          <div class="block">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="currentPage"
              :page-sizes="[5, 10, 20, 50]"
              :page-size="pagesize"
              layout=" prev, pager, next ,sizes, jumper"
              :total="counts"
              background
            >
            </el-pagination></div
        ></el-col>
      </el-row>
      <!-- 弹窗 -->
      <el-dialog
        :title="showTitle"
        :visible.sync="dialogFormVisible"
        width="25%"
      >
        <el-form
          :model="form"
          :rules="rules"
          label-position="right"
          center
          ref="addDirectorys"
        >
          <el-form-item
            label="所属学科"
            :label-width="formLabelWidth"
            prop="subjectName"
          >
            <el-select
              placeholder="请选择"
              v-model="form.subjectName"
              style="width: 260px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="目录名称"
            :label-width="formLabelWidth"
            prop="directoryName"
          >
            <el-input
              v-model="form.directoryName"
              autocomplete="off"
              placeholder="请输入目录名称"
              style="width: 260px"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="btnCancel">取 消</el-button>
          <el-button type="primary" @click="btnOk">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  list,
  add,
  update,
  detail,
  remove,
  changeState
} from '../../api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  data() {
    return {
      id: this.$route.query.id,
      name: this.$route.query.name,
      flag: '',
      list: [],
      options: [],
      search: '',
      currentPage: 1,
      pagesize: 10,
      counts: 0,
      state: '',
      byID: '',
      subjectID: '',
      form: {
        subjectName: '',
        directoryName: ''
      },
      rules: {
        subjectName: [{ required: true }],
        directoryName: [
          { required: true, message: '目录名称不能为空', triiger: 'blur' }
        ]
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  methods: {
    // 获取列表
    async getList() {
      const { data } = await list({
        page: this.currentPage,
        pagesize: this.pagesize
      })
      // console.log(data);
      this.list = data.items
      this.counts = data.counts
    },
    handleSizeChange(list) {
      this.pagesize = list
      this.getList()
    },
    handleCurrentChange(currentPage) {
      // console.log(`当前页: ${currentPage}`);
      this.currentPage = currentPage
      this.getList()
    },

    // 添加目录
    async showDialog() {
      this.flag = true
      const { data } = await simple()
      this.dialogFormVisible = true
      // console.log(res.data);
      this.options = data
    },
    // 点击确定弹窗
    async btnOk() {
      try {
        await this.$refs.addDirectorys.validate()

        if (this.flag == true) {
          await add({
            directoryName: this.form.directoryName,
            subjectID: this.form.subjectName
          })
          // console.log(data);
          this.$message.success('操作成功')
        } else {
          // console.log(11);
          await update({
            id: this.byID,
            subjectID: this.subjectID,
            directoryName: this.form.directoryName
          })
          this.$message.success('操作成功')
        }
        this.dialogFormVisible = false
        this.$refs.addDirectorys.resetFields()
        this.getList()
        this.form = {
          subjectName: '',
          directoryName: ''
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 点击取消
    btnCancel() {
      // 重置原来的数据
      this.flag = ''
      this.form = {
        subjectName: '',
        directoryName: ''
      }
      this.$refs.addDirectorys.resetFields()
      this.dialogFormVisible = false
    },
    // 修改
    async editDirectorys(row) {
      this.flag = false
      this.byID = row.id
      this.subjectID = row.subjectID
      const { data } = await simple()
      // console.log(data);
      // 让数据回显
      this.options = data
      this.form.subjectName = row.subjectID
      this.form.directoryName = row.directoryName
      this.dialogFormVisible = true
    },
    // 删除
    async removeDirectorys(id) {
      // console.log(id);
      try {
        await this.$confirm('此操作将永久删除该学科, 是否继续?')
        await remove({ id })
        this.$message.success('删除员工成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 清空
    reset() {
      this.search = ''
      this.state = ''
      this.getList()
    },
    // 搜索
    async searchDir() {
      let res
      if (this.state) {
        res = await list({
          page: this.currentPage,
          pagesize: this.pagesize,
          directoryName: this.search,
          state: this.state
        })
      } else {
        res = await list({
          page: this.currentPage,
          pagesize: this.pagesize,
          directoryName: this.search
        })
      }
      // console.log(res);
      // 将请求的数据渲染到页面中
      this.list = res.data.items
    },
    // 修改启用和禁用
    async changeState(id) {
      let data = {}
      this.list.forEach((item) => {
        if (item.id === id) {
          item.state = item.state === 0 ? 1 : 0
          data = item
        }
      })
      await changeState(data)
      this.getList()
    },
    // 跳转后
    async regetlist() {
      const { data } = await list({
        subjectID: this.id,
        page: this.currentPage,
        pagesize: this.pagesize
        // directoryName: this.name,
      })
      // console.log(data);
      this.list = data.items
      this.counts = data.counts
    }
  },
  created() {
    // this.getList();
    // this.regetlist();
    if (this.id) {
      this.regetlist()
    } else {
      this.getList()
    }
  },
  watch: {
    counts(value) {
      this.counts = value
    }
  },
  computed: {
    showTitle() {
      return this.flag ? '新增目录' : '修改目录'
    }
  }
}
</script>

<style scoped >
::v-deep .el-dialog .el-dialog__body {
  padding: 30px 10px 0 0;
}
.container {
  padding: 20px;
}
</style>
